public class binarysearch {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int n = 2;
        int i = 0;
        int j = array.length - 1;
        boolean flg = false;
        while (i <= j){
            int x = (i + j)/2;
            if(array[x] < n){
                i = x + 1;
            }
            if(array[x] > n){
                j = x - 1;
            }
            if(array[x] == n){
                System.out.println("查找的数是第"+ (x + 1) +"个");
                flg = true;
                break;
            }
        }
        if(!flg){
            System.out.println("找不到你要的数字");
        }
    }
}
